<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="房间名称" prop="room">
						<el-input v-model="form.room" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="房间面积" prop="room_size">
						<el-input v-model="form.room_size" placeholder="亩/平米" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="所属楼宇" prop="building_id">
						<!-- <el-input v-model="form.building_id" clearable maxlength="80"></el-input> -->
						<el-select v-model="form.building_id" placeholder="所属楼宇" @change="onSelectCity" clearable filterable style="width: 100%">
							<el-option
							v-for="item in buildingList"
							:key="item.id"
							:label="item.name"
							:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="租金定价" prop="rental_pricing">
						<el-input v-model="form.rental_pricing" placeholder="元/平米/天" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="所在楼层/楼宇总层数" prop="floor">
						<el-input v-model="form.floor" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="物业费" prop="strata_fees">
						<el-input v-model="form.strata_fees" placeholder="元/平米/天" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="所属园区" prop="park_id">
						<!-- <el-input v-model="form.park_id" clearable maxlength="80"></el-input> -->
						<el-select v-model="form.park_id" placeholder="所属园区" @change="onSelectCity" clearable filterable style="width: 100%">
							<el-option
							v-for="item in parkList"
							:key="item.id"
							:label="item.name"
							:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="装修状态" prop="renovation_type">
						<el-input v-model="form.renovation_type" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<!-- <el-col :lg="12">
					<el-form-item label="所属片区" prop="area_id">
						<el-input v-model="form.area_id" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col> -->
				<el-col :lg="12">
					<el-form-item label="招商政策" prop="investment_policy">
						<el-input v-model="form.investment_policy" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="功能类别" prop="function_type">
						<el-input v-model="form.function_type" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="是否推荐" prop="recommended">
						<sc-select v-model="form.recommended" :params="{code:'supervisionLevel'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="产业定位(聚合)" prop="industry_positioning">
				<el-input v-model="form.industry_positioning" clearable maxlength="80"></el-input>
			</el-form-item>
			<!-- <el-form-item label="空间图片上传" prop="spatial_pictures">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'supervision'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传图片</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<el-form-item label="附件上传" prop="attachment_upload">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'supervision'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file> 
			</el-form-item>-->
			<el-form-item label="空间介绍" prop="space_introduction">
				<sc-editor v-model="form.space_introduction" placeholder="请输入" :templates="templates" :height="400" clearable  style="width: 100%;"></sc-editor>
			</el-form-item>
			<msg-box-check-box v-model:msgType="form.msgType" code="supervision"></msg-box-check-box>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import AttachMenu from '@/views/module/attachMenu'
import {defineAsyncComponent} from "vue";
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	emits: ['success', 'closed'],
	components: {
		msgBoxCheckBox,
		selectUserDialog,
		AttachMenu,
		scEditor
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg:false,
			mode: "add",
			templates: [],
			fileList:[],
			userList:[],
			buildingList:[],
			parkList:[],
			apiObj:this.$API.file.attach.uploadFile,
			titleMap: {
				add: '房间信息（表单）',
				edit: '房间信息（展示页）',
			},
			form: {
				id:'',
				room:'',
				room_size:'',
				building_id: '',
				rental_pricing: '',
				floor: '',
				strata_fees:"",
				park_id:"",
				renovation_type:'',
				area_id:'',
				investment_policy: '',
				function_type: '',
				recommended:'',
				industry_positioning:'',
				spatial_pictures:'',
				attachment_upload:'',
				space_introduction:''
			},
			rules: {
				name: [
					{required: true, message: '空间名称不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.getSpaceList();
		this.getParkList();
		this.getBuildingList();
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		publishDateAfter(time){
			return time.getTime()<(Date.now()-24*60*60*1000)
		},
		async getSpaceList(){
			let res = await this.$API.attractinvestment.space.getSpaceList.get();
			if (res.code == 200) {
				this.userList = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async getParkList(){
			let res = await this.$API.attractinvestment.park.getParkList.get();
			if (res.code == 200) {
				this.parkList = res.data.list;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async getBuildingList(){
			let res = await this.$API.attractinvestment.building.getBuildingList.get();
			if (res.code == 200) {
				this.buildingList = res.data.list;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					delete this.form.industry_positioning;
					if (this.mode == "add") {
						res = await this.$API.attractinvestment.space.insertSpace.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.attractinvestment.space.updateSpace.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.id = data.id
			this.form.room = data.room
			this.form.room_size = data.room_size
			this.form.building_id = data.building_id
			this.form.rental_pricing = data.rental_pricing
			this.form.floor = data.floor
			this.form.strata_fees = data.strata_fees
			this.form.park_id = data.park_id
			this.form.renovation_type = data.renovation_type
			this.form.area_id = data.area_id
			this.form.investment_policy = data.investment_policy
			this.form.function_type = data.function_type
			this.form.recommended = data.recommended  
			this.form.industry_positioning = data.industry_positioning
			this.form.attachment_upload = data.attachment_upload
			this.form.space_introduction = data.space_introduction
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
